module tb_add_vector();

   logic [63:0] sum_diff;
   logic [7:0] 	carrys_borrows;
   
   logic [63:0] op_a, op_b;
   logic [3:0] 	mode;
   logic [7:0] 	add_sub_ctrl;
   
   add_sub_vector #(.width(64), 
					.granu(8)) U_add_sub_vector (
												 // Outputs
												 .o_sum_diff			(sum_diff),
												 .o_carrys_borrows		(carrys_borrows),
												 // Inputs
												 .i_a					(op_a),
												 .i_b					(op_b),
												 .i_mode				(mode),
												 .i_add_sub_ctrl		(add_sub_ctrl) );
   
   
   initial
   	 begin
		logic [63:0] correct_result;
		logic [7:0]  cb;
		logic [7:0]  carrys_borrows_mask;
		
		for(int i=0;i<4;i++)	// mode
		  begin
			 $display("=============== test %d-bit mode ===============", (2**i)*8 );
			 for(int loop=0;loop<1000;loop++)
			   begin
				  mode = 1 << i;
				  op_a = {$random, $random};
				  op_b = {$random, $random};
				  add_sub_ctrl = $random;

				  cb = '0;
				  carrys_borrows_mask = '0;
				  
				  case((2**i)*8)
					64:	// 64 bit mode
					  begin
						 {cb[7], correct_result} = add_sub_ctrl[7] ? op_a - op_b : op_a + op_b;
						 carrys_borrows_mask[7] = 1'b1;
					  end
					32:
					  begin
						 for(int k=0;k<2;k++)
						   begin
							  {cb[k*4+3], correct_result[k*32+:32]} = add_sub_ctrl[k*4+3] ? op_a[k*32+:32] - op_b[k*32+:32] : op_a[k*32+:32] + op_b[k*32+:32];
							  carrys_borrows_mask[k*4+3] = 1'b1;
						   end
					  end
					16:
					  begin
						 for(int k=0;k<4;k++)
						   begin
							  {cb[k*2+1], correct_result[k*16+:16]} = add_sub_ctrl[k*2+1] ? op_a[k*16+:16] - op_b[k*16+:16] : op_a[k*16+:16] + op_b[k*16+:16];
							  carrys_borrows_mask[k*2+1] = 1'b1;
						   end
					  end
					8:
					  begin
						 for(int k=0;k<8;k++)
						   begin
							  {cb[k], correct_result[k*8+:8]} = add_sub_ctrl[k] ? op_a[k*8+:8] - op_b[k*8+:8] : op_a[k*8+:8] + op_b[k*8+:8];
							  carrys_borrows_mask[k] = 1'b1;
						   end
					  end
				  endcase // case ((2**i)*8)
				  
				  #1;
				  $display("%x +/- %x => %x, carrys_borrows:%x  <-> correct:%x,%x  =>  %s",
						   op_a, op_b, sum_diff, carrys_borrows/* & carrys_borrows_mask*/,
						   correct_result, cb,
						   {carrys_borrows & carrys_borrows_mask, sum_diff} === {cb,correct_result} ? "equal" : "not equal" );
			   end // for (int i=0;i<4;i++)
			 // $display("=============== passed ===============");
		  end // for (int i=0;i<4;i++)
	 end // initial begin
   
		

endmodule // tb_add_vector
